package com.midou.administration.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class TokenUtils {
    public static final String JWT_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCFUKn3emK0CecoBoqv90GUbiLtd/K/IN5Jb8kvFSi+15PepNDrdeS2+gqvJoyaGs+y8xQgUgqGVF06txa1gU/XGudLCwI31SdEQCDnajpGxNYY9nt7fnvitqUg6QwDkeFjMaKMdgHBB5J1mx/+tdvIb2IUZWHBa+HjSuYSIfNrpXH0+7c5PMF3kwT9Ozs3kztW5SNmpI1ABdTThXfe77BiHt23cE+ZBLQhWR+BqhkqROjjt/7f9Sw7XN6zbb4pmo9QM4BKmiL7tcoNR0kqMQie+7GrfMfXnagjCA5mbTvEwFMtugF57zk8pVFmY4YS0gB2Sc/xiRKiHWMkiFTyQqmpAgMBAAECggEAetjc9q6xW6QwfSKiFAqtRMDEsLyyVMtN6q/23MsLs+hlW2nXFtKZgr8gr0u/jpYcRJNYPXw8NJJzIpLqezEAyIADGJS5Y4eo2myZpYWu0924SGKBYUgpkr2ppoz5xCReZBCt7+8iKKT4oiTjM8yTDb0NzSurDBiEm/U90wG0FMGS7QZufn+SyGpPf9BqounqlOV0biRbKvbpxdIRD9VDjpg8xTY3PcOT6rpCNdicMTPwKaOVPg708ROk7SWBieOPMEhH6EgAYOae//wG/E9Hq/+usxHWHszP9vYPTEUjFLUGvMT+dBiSHCxDusFU2mR8s6XCJX3b+IUMNLmYQNxrTQKBgQDd7fFyBHyEAmJqdK8Qs1xNq6UZ+7BBqz9E0w9p7qShEkHXck/Zj4Pipz1G2KOYOQBspvO5AyBIOTuAP/Cy6rtIVfgO5d2m71NYCffhGuWcpid/hgnNKJXVaBwGQxuhs0ZfqqpxKURhu18LKaoxnPG1eBl48LwsQEVIcEJ5kSZhTwKBgQCZyBWnedApJPLZghD+m2iDvYUv2K7Ledclka2/Ww+CEmktq4ZxKY0WYIis7yCQrbat514210pTKPy2xKoOL5GeH9bIJs7488F2wygJ41N+M/MGAqt1nP1Onfx3KDaHoiIbn2VMI2Gt53NnU7uzgmnuFL4pgSIU3sOwtCNY1PTXhwKBgQCb4RVF0IDk+AfIXkzYniKnK3i711zlwzKdVOHutgBMYlZ8zX3ogjVQbcdMi73cnaz+pAVsXhW+pPR2EHMwOE9zb27ch/g32MHhhJ7nLc05SjtUBwG7aFZVntqQS3KNES7M40L0WNf/DWykmo9Aacwcek7p8604J6yCRNdsK9eZYQKBgQCNWPl+XFrW1p1ZC1IvlAy2umZHGjVf1XfJkmNPNZXFisKeNcB3FygVX1mBCrK2CNWWdsmEQtKQKWZPX+im7VY9LyquAe3Pqc0zKiINJKPDpmBEMAQFMO0ra894fr/QX1hWZ2YmW4cW31yVpsMqNj7DlNFNOOJoqHDaZFjw95ys1QKBgQC3er7y79Vde1gExfcHWCze6weKPiR4alh2hyWCm3Z+ZRXQsQRWMlzb3/bxExxvNWN1eQD/kL2J8q1KrGoqXM6sljrw8F83j6w/36HcIWfwhvXy4xH9wsLXkhfAu4oEe0wyH3FabbQc7ezVqLb+RsPSP/zCMnkGD213Q1Lr9NYGlA==";
    //私钥和加密算法
    public static Algorithm algorithm = Algorithm.HMAC512(JWT_KEY);

    public static String getToken(Integer cid,Integer bid){
        try {
            // 设置过期时间
            Date expiredDate = new Date(System.currentTimeMillis() + (1000 * 60 * 1));
            //生成的token字符串
            String token = JWT.create()
                    .withIssuer("MrChen") //token的签发者
                    .withClaim("cid", cid) //载荷中存储的信息
                    .withClaim("bid",bid)
                    .withExpiresAt(expiredDate) //token的有效时间
                    .sign(algorithm);   //签名
            return token;
        }catch (Exception e){
            return null;
        }

    }
    public static Claim shopping(String token){
        try{
            JWTVerifier verifier = JWT.require(algorithm).withIssuer("MrChen").build();
            verifier.verify(token); //检验token是否合法，是否被篡改
            DecodedJWT decodedJWT = JWT.decode(token);
            Claim claim = decodedJWT.getClaim("userid");//从token中获取信息
            return claim;
        }catch (Exception e){
            e.getMessage();
            return null;
        }

    }

/*    public static void main(String[] args) {
        String token = getToken(1);
        System.out.println(token);

    }*/
}
